
/* STANDARD INCLUDE FILES */
{mfdeclre.i}
{gplabel.i}  /* EXTERNAL LABEL INCLUDE */
{pxsevcon.i}
{pxpgmmgr.i}

/* ********** Begin Translatable Strings Definitions ********* */
&SCOPED-DEFINE popomth_p_1 "Comments"
/* MaxLen: Comment: */
/* ********** End Translatable Strings Definitions ********* */

/* SHARED FRAMES */
define shared frame chead.
define shared frame c.
define shared frame d.

/* SHARED VARIABLES */
define shared variable desc1               like pt_desc1.
define shared variable desc2               like pt_desc2.
define shared variable line                like sod_line.
define shared variable del-yn              like mfc_logical.
define shared variable po_recno            as   recid.
define shared variable vd_recno            as   recid.
define shared variable old_qty_ord         like pod_qty_ord.
define shared variable ext_cost            like pod_pur_cost.
define shared variable pod_recno           as   recid.
define shared variable podcmmts            like mfc_logical label {&popomth_p_1}.
define shared variable sngl_ln             like poc_ln_fmt.
define shared variable mfgr                like vp_mfgr.
define shared variable mfgr_part           like vp_mfgr_part.
define shared variable continue            like mfc_logical.
define shared variable st_qty              like pod_qty_ord.
define shared variable st_um               like pod_um.
define shared variable old_um              like pod_um.
define shared variable clines              as   integer initial ?.
define shared variable blanket             like mfc_logical.
define shared variable new_db              like si_db.
define shared variable old_db              like si_db.
define shared variable new_site            like si_site.
define shared variable old_site            like si_site.
define shared variable podnbr              like pod_nbr.
define shared variable podline             like pod_line.
define shared variable podreqnbr           like pod_req_nbr.
define shared variable base_cost           like pod_pur_cost.
define shared variable line_opened         as   logical.
define shared variable price_came_from_req as   logical   no-undo.
define shared variable old_pod_status      like pod_status.

/* REQUISITION BY SITE WORKFILE INCLUDE FILE */
{poprwkfl.i}

/* LOCAL VARIABLES */
define variable requm             as   character no-undo.
define variable open_qty          as   decimal   no-undo.
define variable ok                as   logical   no-undo.
define variable yn                like mfc_logical initial no.
define variable itemAvailable     as   logical   no-undo.
define variable supplierAvailable as   logical   no-undo.
define variable itemUM            as   character no-undo.
define variable dummyDecimal      as   decimal   no-undo.
define variable dummyCharacter    as   character no-undo.
define variable l_conf_ship     as   integer     no-undo.
define variable l_conf_shid     like abs_par_id  no-undo.
define variable l_shipper_found as   integer     no-undo.
define variable l_save_abs      like abs_par_id  no-undo.
define variable using_grs         like mfc_logical no-undo.
define variable use-log-acctg     as logical no-undo.

/* VARIABLES FOR CONSIGNMENT INVENTORY */
{pocnvars.i}

/* PROCEDURES AND FRAMES FOR CONSIGNMENT INVENTORY */
{pocnpo.i}

if c-application-mode = "API" then do on error undo,return error:
    /* COMMON API CONSTANTS AND VARIABLES */
    {mfaimfg.i}
    /* PURCHASE ORDER API TEMP-TABLE, NAMED USING THE "Api" PREFIX */
    {popoit01.i}
    /* GET HANDLE OF API CONTROLLER */
    {gprun.i ""gpaigh.p""
        "(output ApiMethodHandle,
        output ApiProgramName,
        output ApiMethodName,
        output apiContextString)"}

    if global-beam-me-up then undo, leave.

    /* GET LOCAL PO MASTER TEMP-TABLE */
    create ttPurchaseOrder.
    run getPurchaseOrderRecord in ApiMethodHandle (buffer ttPurchaseOrder).

    /* GET LOCAL PO MASTER TEMP-TABLE */
    create ttPurchaseOrderDet.
    run getPurchaseOrderDetRecord in ApiMethodHandle (buffer ttPurchaseOrderDet).
end.  /* If c-application-mode = "API" */

/* PURCHASE ORDER MAINTENANCE SINGLE LINE ITEMS */
{mfpomtb.i} /* Defines forms c & d */

/*V8:HiddenDownFrame=c */
using_grs = can-find(mfc_ctrl where mfc_field   = "grs_installed" and mfc_logical = yes).

{gprun.i ""lactrl.p"" "(output use-log-acctg)"}

if global-beam-me-up then undo, leave.

/*============================================================================*/
/* ****************************** Main Block ******************************** */
/*============================================================================*/

for first po_mstr where recid(po_mstr) = po_recno:
end.
for first vd_mstr where recid(vd_mstr) = vd_recno no-lock:
end.
for first pod_det where recid(pod_det) = pod_recno:
end.

{pxrun.i &PROC       = 'getBasicItemData'
    &PROGRAM    = 'ppitxr.p'
    &PARAM      = "(input pod_part,
    output dummyCharacter,
    output dummyCharacter,
    output dummyDecimal,
    output dummyCharacter,
    output dummyCharacter,
    output itemUM,
    output dummyCharacter)"
    &NOAPPERROR = true
    &CATCHERROR = true}
    
if return-value = {&SUCCESS-RESULT} then
    itemAvailable = true.

{pxrun.i &PROC       = 'processRead'
    &PROGRAM    = 'ppsuxr.p'
    &PARAM      = "(input pod_part,
    input po_vend,
    input pod_vpart,
    buffer vp_mstr,
    input FALSE,
    input FALSE)"
    &NOAPPERROR = true
    &CATCHERROR = true}
    
if return-value = {&SUCCESS-RESULT} then supplierAvailable = true.

base_cost = 0.

setc-1:
do on error undo, retry:
    if global-beam-me-up then undo, leave.
    /* DO NOT RETRY WHEN PROCESSING API REQUEST */
    if retry and c-application-mode = "API" then
        undo setc-1, return error.

    /* VALIDATES PO LINE UNIT OF MEASURE */
    {pxrun.i &PROC       = 'validatePOLineUM'
        &PROGRAM    = 'popoxr1.p'
        &PARAM      = "(input pod_um)"
        &NOAPPERROR = true
        &CATCHERROR = true}

    if return-value <> {&SUCCESS-RESULT} then do:
        undo setc-1, return error.
    end.  /* if return-value <> {&SUCCESS-RESULT} then do: */

    if (po_stat <> "c" and po_stat <> "x") and (pod_status <> "c" and pod_status <> "x") and 
        pod_type = "" and blanket = false then do:
        /* UPDATE PART MASTER MRP FLAG */
        {pxrun.i &PROC       = 'updateItemForMRP'
            &PROGRAM    = 'popoxr1.p'
            &PARAM      = "(input pod_part,
            input pod_site)"
            &NOAPPERROR = true
            &CATCHERROR = true}
    end. /* IF (po_stat <> "C" AND po_stat <> "X") AND .... */

    if old_pod_status = "" then old_pod_status = pod_status. 

    if not new pod_det then do:
        if pod_cmtindx <> 0 then podcmmts = yes.

        if pod_qty_ord <> old_qty_ord and (po_stat = "c" or po_stat = "x" or pod_status  = "c" or 
            pod_status  = "x") then do:

            {pxrun.i &PROC='reOpenPurchaseOrder'
                &PROGRAM='popoxr.p'
                &PARAM      = "(input pod_nbr)"
                &NOAPPERROR = true
                &CATCHERROR = true}

            assign
                pod_status  = ""
                line_opened = true.

            {pxrun.i &PROC       = 'reOpenRequisition'
                &PROGRAM    = 'rqgrsxr.p'
                &PARAM      = "(input pod_req_nbr,
                input pod_req_line)"
                &NOAPPERROR = true
                &CATCHERROR = true}
        end.

        {pxrun.i &PROC       = 'validateOrderQtyAgainstRcptQty'
            &PROGRAM    = 'popoxr1.p'
            &PARAM      = "(input pod_qty_ord,
            input old_qty_ord,
            input pod_qty_rcvd)"
            &NOAPPERROR = true
            &CATCHERROR = true}
            
        if pod_blanket <> "" and pod_qty_ord <> old_qty_ord then do:
            {pxrun.i &PROC='validateOrderQtyAgainstBlanketOrderOpenQty'
            &PROGRAM='popoxr1.p'
            &PARAM="(input pod_blanket,
            input pod_blnkt_ln,
            input pod_qty_ord,
            input old_qty_ord,
            input old_pod_status)"
            &NOAPPERROR=true &CATCHERROR=true}
            
            if return-value <> {&SUCCESS-RESULT} then do:
                next-prompt pod_qty_ord with frame c.
                undo setc-1, retry.
            end. /* IF RETURN-VALUE <> {&SUCCESS-RESULT} */
        end. /* IF POD_BLANKET <> "" */

        if pod_qty_ord < 0 and pod_consignment then do:
            undo setc-1, return error.
        end. /* IF pod_qty_ord < 0 */

        if old_qty_ord <  0 and pod_qty_ord >= 0 then do:
            run 'initializeSuppConsignDetailFields'
                (input po_vend,
                input pod_part,
                input po_consignment,
                input po_max_aging_days,
                output pod_consignment,
                output pod_max_aging_days).
        end. /* IF old_qty_ord < 0 */
    end. /* IF NOT NEW pod_det */

    /* Only need to calc UM conversion on new line. */
    if (new pod_det) then do:
        {pxrun.i &PROC       = 'calculatePOLineUMConversion'
            &PROGRAM    = 'popoxr1.p'
            &PARAM      = "(buffer pod_det)"
            &NOAPPERROR = true
            &CATCHERROR = true}
    end. /*IF (NEW pod_det)*/

/* GET DEFAULT PURCHASE ORDER UNIT COST */

    if (c-application-mode <> "API" and pod_um entered or new pod_det)
        or (c-application-mode = "API" and ttPurchaseOrderDet.um <> ? or new pod_det)
        and itemAvailable then do:

        if price_came_from_req then do:
            
            {pxrun.i &PROC       = 'computeBaseCost'
                &PROGRAM    = 'popoxr1.p'
                &PARAM      = "(input pod_part,
                input pod_site,
                input pod_um_conv,
                input po_ord_date,
                output base_cost)"
                &NOAPPERROR = true
                &CATCHERROR = true}

            for first rqd_det fields(rqd_line rqd_nbr rqd_um_conv) where rqd_nbr = pod_req_nbr
                and rqd_line = pod_req_line no-lock:
                pod_pur_cost = (pod_pur_cost / rqd_um_conv) * pod_um_conv.
            end. /* FOR FIRST rqd_det */
        end. /* IF price_came_from_req */
        else do:
            {pxrun.i &PROC    = 'getStandardCost'
                &PROGRAM = 'ppicxr.p'
                &PARAM   = "(input pod_part,
                input pod_site,
                input po_ord_date,
                output glxcst)"}

            base_cost = glxcst * pod_um_conv.

            if not supplierAvailable then do:
                {pxrun.i &PROC       = 'calculatePOCostInForeignCurr'
                    &PROGRAM    = 'popoxr1.p'
                    &PARAM      = "(input glxcst,
                    input pod_um_conv,
                    input po_curr,
                    input po_ex_rate,
                    input po_ex_rate2,
                    output pod_pur_cost)"
                    &NOAPPERROR = true
                    &CATCHERROR = true}
            end. /* IF NOT AVAILABLE vp_mstr */
            else if new pod_det then do:
                {pxrun.i &PROC    = 'calculatePOCurrCostFromSuppItem'
                    &PROGRAM = 'popoxr1.p'
                    &PARAM   = "(input pod_part,
                    input po_vend,
                    input pod_vpart,
                    input itemUM,
                    input pod_um,
                    input pod_qty_ord,
                    input pod_um_conv,
                    input po_curr,
                    input po_ex_rate,
                    input po_ex_rate2,
                    input glxcst,
                    output pod_pur_cost)"
                    &NOAPPERROR    = true
                    &CATCHERROR    = true}
            end. /* ELSE IF NEW pod_det */
        end. /* END - ELSE PORTION OF IF price_came_from_req */
    end. /* END - if (pod_um entered OR NEW pod_det) */

    if new pod_det and not price_came_from_req then do:
        {pxrun.i &PROC       = 'calculateCostFromUMConvertedSupplierItem'
            &PROGRAM    = 'popoxr1.p'
            &PARAM      = "(input pod_part,
            input po_vend,
            input pod_vpart,
            input pod_um,
            input pod_qty_ord,
            input po_curr,
            input-output pod_pur_cost)"
            &NOAPPERROR = true
            &CATCHERROR = true}
    end. /* IF NEW pod_det */

    if  using_grs and pod_req_nbr > "" and po_type <> "b" and 
        pod_req_line <>  0 then do:

        {pxrun.i &PROC       = 'updateRequisitionCrossReference'
            &PROGRAM    = 'rqgrsxr1.p'
            &PARAM      = "(input pod_site,
            input pod_req_nbr,
            input pod_req_line,
            input pod_nbr,
            input pod_line,
            input pod_qty_ord,
            input pod_um,
            output requm,
            output ok)"
            &NOAPPERROR = true
            &CATCHERROR = true}

        if return-value <> {&SUCCESS-RESULT} then do:
            undo, return error.
        end.
            
        {pxrun.i &PROC       = 'validateRequisitionLineOpenQuantity'
            &PROGRAM    = 'rqgrsxr1.p'
            &PARAM      = "(input pod_req_nbr,
            input pod_req_line,
            input pod_site)"
            &NOAPPERROR = true
            &CATCHERROR = true}

        if return-value <> {&SUCCESS-RESULT} then do:
            undo setc-1, return error.
        end.

        if return-value = {&SUCCESS-RESULT} then do:
            {pxrun.i &PROC      = 'getRequisitionLineOpenQuantity'
                &PROGRAM   = 'rqgrsxr1.p'
                &PARAM     = "(input pod_req_nbr,
                input pod_req_line,
                input pod_site,
                output open_qty,
                output requm)"
                &NOAPPERROR = true
                &CATCHERROR = true}
        end. /* IF return-value ... */

        {pxrun.i &PROC       = 'validatePOLineQuantityOrdered'
            &PROGRAM    = 'popoxr1.p'
            &PARAM      = "(input pod_qty_ord)"
            &NOAPPERROR = true
            &CATCHERROR = true}

        if return-value <> {&SUCCESS-RESULT} then do:
            undo setc-1, return error.
        end.

        {pxrun.i &PROC       = 'updateMRPForRequisition'
            &PROGRAM    = 'rqgrsxr1.p'
            &PARAM      = "(input pod_site,
            input pod_req_nbr,
            input pod_req_line)"
            &NOAPPERROR = true
            &CATCHERROR = true}
    end. /* IF using_grs... */
    if global-beam-me-up then undo, leave.
end.  /* setc-1 */
